package cn.wxManage.goodsManage.service.impl;

import cn.common.common.model.ServerResponse;
import cn.wxManage.common.dao.ImageInfoMapper;
import cn.wxManage.goodsManage.dao.GoodsCollectMapper;
import cn.wxManage.goodsManage.dao.GoodsMapper;
import cn.wxManage.goodsManage.dao.GoodsStyleMapper;
import cn.wxManage.goodsManage.model.Goods;
import cn.wxManage.goodsManage.service.IGoodsManageService;
import cn.wxManage.user.model.UserInfo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Zhang on 2018/12/12.
 */
@Service
public class GoodsManageService implements IGoodsManageService {
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    GoodsStyleMapper goodsStyleMapper;

    @Autowired
    ImageInfoMapper imageInfoMapper;

    @Autowired
    GoodsCollectMapper goodsCollectMapper;

    @Override
    public ServerResponse<?> getGoodsList(Goods goods) {
        List<Goods> goodsList = goodsMapper.getGoodsList(goods);
        return ServerResponse.createBySuccess(goodsList);
    }

    @Override
    public ServerResponse<?> getHotGoodsList() {
        List<Goods> goodsList = goodsMapper.getHotGoodsList();
        for (Goods goods : goodsList){
            goods.setMainImgPath(imageInfoMapper.getGoodsMain(goods.getId()));
        }
        return ServerResponse.createBySuccess(goodsList);
    }

    @Override
    public ServerResponse<?> getSearchGoodsList(String name) {
        List<Goods> goodsList = goodsMapper.getSearchGoodsList(name);
        for (Goods goods : goodsList){
            goods.setMainImgPath(imageInfoMapper.getGoodsMain(goods.getId()));
        }
        return ServerResponse.createBySuccess(goodsList);
    }

    @Override
    public ServerResponse<?> getGoodsInfo(Integer id) {
        Goods goods = goodsMapper.getGoodsInfo(id);
        goods.setStyleList(goodsStyleMapper.getGoodsStyleByGoodsId(id));
        goods.setMainImgPath(imageInfoMapper.getGoodsMain(id));
        goods.setDetailImgPath(imageInfoMapper.getGoodsDetail(id));
        goods.setCollect(goodsCollectMapper.checkIsCllect(getUserInfo().getUserid(),id));
        return ServerResponse.createBySuccess(goods);
    }

    private UserInfo getUserInfo(){
        return (UserInfo) request.getAttribute("userInfo");
    }
}
